package com.teamall.web.app.controller;

/**
 * ${DESCRIPTION}
 *
 * @author
 * @create 2017-08-23 21:48
 **/

import com.teamall.framework.exception.BaseAppException;
import com.teamall.web.base.vo.ResResult;
import com.teamall.web.fpgz.db.po.CodeVillageInfoPO;
import com.teamall.web.fpgz.service.ICodeVillageInfoService;
import com.teamall.web.main.db.po.CodeStatTownPO;
import com.teamall.web.main.service.ICodeStatTownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/app/sys")
public class AppSysController {
    @Autowired
    private ICodeVillageInfoService codeVillageInfoService;
    @Autowired
    private ICodeStatTownService codeStatTownService;
    @RequestMapping("/getTownVillageInfo")
    @ResponseBody
    public ResResult getTownVillageInfo() throws BaseAppException {
        ResResult resResult = new ResResult();
        List<CodeStatTownPO> codeStatTownPOList = codeStatTownService.selectByArg(new CodeStatTownPO());
        List<Map> resultList = new ArrayList<Map>();
        for(CodeStatTownPO codeStatTownPO:codeStatTownPOList){
            Map resultMap = new HashMap();
            resultMap.put("id",codeStatTownPO.getTownId());
            resultMap.put("name",codeStatTownPO.getTownName());
            resultMap.put("infoList",new ArrayList<>());
            resultList.add(resultMap);
        }
        List<CodeVillageInfoPO> codeVillageInfoPOList = codeVillageInfoService.selectByArg(new CodeVillageInfoPO());
        for (CodeVillageInfoPO codeVillageInfoPO:codeVillageInfoPOList){
            for (int i = 0; i < resultList.size(); i++) {
                if(resultList.get(i).get("id") == codeVillageInfoPO.getTownId()){
                    Map resultMap = new HashMap();
                    resultMap.put("id",codeVillageInfoPO.getVillageId());
                    resultMap.put("name",codeVillageInfoPO.getVillageName());
                    ((List)resultList.get(i).get("infoList")).add(resultMap);
                }
            }
        }
        resResult.setRspData(resultList);
        return resResult;
    }
    @RequestMapping("/getMyTownVillageInfo")
    @ResponseBody
    public ResResult getMyTownVillageInfo() throws BaseAppException{
        ResResult resResult = new ResResult();
        Map resultMap = new HashMap();
        resultMap.put("townId",1);
        resultMap.put("villageId",1);
        resResult.setRspData(resultMap);
        return resResult;
    }
    @RequestMapping("/test")
    @ResponseBody
    public ResResult test() throws BaseAppException{
        ResResult resResult = new ResResult();
        return resResult;
    }
}
